import java.util.ArrayList;
import java.util.Scanner;

public class Library {
    public ArrayList<Book> books = new ArrayList<Book>();
    public ArrayList<Movie> movies = new ArrayList<Movie>();
    public User currentUser;

    public void start(){
        setup();
        welcomeScreen();
        menu();
    }

    public void addBook(Book book){
        books.add(book);
    }

    public void setup(){
        Book book1 = new Book("The stranger", "Albert Camus");
        Book book2 = new Book("To Kill a Mockingbird", "Harper Lee");
        Book book3 = new Book("Test book", "Test author");

        addBook(book1);
        addBook(book2);
        addBook(book3);

        Movie movie1 = new Movie("The silence of the lambs", "Jonathan Demme", 1991, 9);
        Movie movie2 = new Movie("Beetlejuice", "Tim Burton", 1998, 9);

        addMovie(movie1);
        addMovie(movie2);

        new User("111-1234", "random_password", "charlie", "charlie@gmail.com", 1111);
        new User("123-1111", "other_password", "test", "test@test.com", 0000);
    }


    public Book getBook(int index){
        return books.get(index - 1);
    }

    public void addMovie(Movie movie){
        movies.add(movie);
    }

    public Movie getMovie(int index){
        return movies.get(index - 1);
    }

    public User getCurrentUser(){
        return currentUser;
    }

    public void welcomeScreen(){
        System.out.println("**************************");
        System.out.println("* Welcome to Biblioteca! *");
        System.out.println("**************************");
        System.out.println(books.size());
    }

    public void setUpMenu(){
        System.out.println("*           Menu         *");
        System.out.println("*    =================   *");
        if(UserLoggedIn()){
            System.out.println("*    0. Logout           *");
        } else {
            System.out.println("*    0. Login            *");
        }
        System.out.println("*    1. List of Books    *");
        System.out.println("*    2. List of Movies   *");
        System.out.println("*    3. Check out book   *");
        System.out.println("*    4. Check out movie  *");
        System.out.println("*    5. Return book      *");
        System.out.println("*    6. Return movie     *");
        System.out.println("*    7. Quit             *");
        if(UserLoggedIn()){
            System.out.println("*   8.See user details    *");
        }
        System.out.println("*    =================   *");
        System.out.println("Please enter your selection");
    }

    Integer selection = 0;

    public void menu(){
        while(selection != 7){
            setUpMenu();
            selection = getMenuSelection();
            selectMenuOption(selection);
        }
    }

    public void selectMenuOption(int selection){
        switch(selection){
            case 0:
                if(UserLoggedIn()){
                    logout();
                } else {
                    Scanner in = new Scanner(System.in);
                    System.out.println("Library Number: ");
                    String libraryNumber = in.nextLine();

                    System.out.println("Password: ");
                    String password = in.nextLine();

                    login(libraryNumber, password);
                };
                break;
            case 1:
                showBooks();
                break;
            case 2:
                showMovies();
                break;
            case 3:
                System.out.println("Which book would you like to borrow?");
                int bookChoice = getNumberInput();
                checkOutBook(bookChoice);
                break;
            case 4:
                System.out.println("Which movie would you like to borrow?");
                int movieChoice = getNumberInput();
                checkOutMovie(movieChoice);
                break;
            case 5:
                System.out.println("Which book would you like to return?");
                int returnBookChoice = getNumberInput();
                returnBook(returnBookChoice);
                break;
            case 6:
                System.out.println("Which movie would you like to return?");
                int returnMovieChoice = getNumberInput();
                returnMovie(returnMovieChoice);
                break;
            case 7:
                System.out.println("Quit");
                System.exit(0);
                break;
            case 8:
                System.out.println("Name: " + currentUser.getName() + ", email address: " + currentUser.getEmailAddress() + ", phone number: " + currentUser.getPhoneNumber());
                break;
            default:
                System.out.println("Select a valid option!");
                break;
        }
    }

    public void checkOutBook(int bookChoice){
        if((bookChoice > 0) && (bookChoice <= books.size())){
            Book book = getBook(bookChoice);
            if(book.isAvailable()) {
                book.setAvailable(false);
                System.out.println("Thank you! Enjoy the book!");
            } else {
                System.out.println("That book is not available");
                book.setAvailable(false);
            }
        } else {
            System.out.println("Please select a valid book");
            return;
        }
    }

    public void checkOutMovie(int movieChoice){
        if(movieChoice > 0 && movieChoice <= movies.size()) {
            Movie movie = getMovie(movieChoice);
            if (movie.isAvailable()) {
                movie.setAvailable(false);
                System.out.println("Thank you! Enjoy the movie!");
            } else {
                System.out.println("That movie is not available");
            }
        } else {
            System.out.println("Please select a valid movie");
            return;
        }
    }

    public int getMenuSelection(){
        Scanner in = new Scanner(System.in);
        return in.nextInt();
    }

    public int getNumberInput(){
        Scanner in = new Scanner(System.in);
        return in.nextInt();
    }

    public void showBooks(){
        int index =  1;

        for (Book book : books){
            if(book.isAvailable()) {
                System.out.println(index + "." + book.getTitle() + " by " + book.getAuthor());
                index++;
            }
        }
    }

    public void showMovies(){
        int index = 1;
        for (Movie movie : movies){
            if(movie.isAvailable()){
                System.out.println(index + "." + movie.getName() + " by " + movie.getDirector() + " realised in " + movie.getYear() + " rated " + movie.getRating());
                index++;
            }
        }
    }

    public void returnBook(int returnBookChoice){
        if(returnBookChoice > 0 && returnBookChoice <= books.size()){
            Book book = getBook(returnBookChoice);
            if (!book.isAvailable()) {
                book.setAvailable(true);
                System.out.println(book.isAvailable());
                System.out.println("Thank you for returning the book.");
            } else {
                System.out.println("That is not a valid book to return.");
            }
            System.out.println("That is not a valid book to return.");
        } else {
            System.out.println("That is not a valid book to return.");
            return;
        }
    }

    public void returnMovie(int returnMovieChoice){
        if(returnMovieChoice > 0 && returnMovieChoice <= movies.size()){
            Movie movie = getMovie(returnMovieChoice);
            if(!movie.isAvailable()){
                movie.setAvailable(true);
                System.out.println("Thank you for returning the movie.");
            } else {
                System.out.println("That is not a valid movie to return.");
            }
        } else {
            System.out.println("That is not a valid movie to return");
            return;
        }

    }

    public void login(String libraryNumber, String password){
        currentUser = User.authenticate(libraryNumber, password);
        if(!UserLoggedIn()){
            System.out.println("Error: Invalid login details");
        }
    }

    public void logout(){
        currentUser = null;
    }

    public boolean UserLoggedIn(){
        return !(getCurrentUser() == null);
    }
}
